﻿using System;
using PTop.Application.Biz;
using PTop.Application.SystemSecurity;
using PTop.Code;
using PTop.Domain.Entity.Article;
using PTop.Domain.IRepository.Article;
using PTop.Repository.Article;
using System.Collections.Generic;
using System.Linq;

namespace PTop.Application.Article
{
    /// <summary>
    /// 评论
    /// </summary>
    public class CommentApp
    {
        private ICommentRepository service = new CommentRepository();

        /// <summary>
        /// 
        /// </summary>
        /// <param name="pagination"></param>
        /// <param name="pid"></param>
        /// <param name="keyword"></param>
        /// <param name="state">审核状态。-1=所有,0=未审核，1=审核通过, 2=未通过</param>
        /// <returns></returns>
        public List<CommentEntity> GetList(Pagination pagination, string pid, string keyword, int state)
        {
            var expression = ExtLinq.True<CommentEntity>();
            if (!string.IsNullOrEmpty(pid))
            {
                expression = expression.And(t => t.F_ArticleId == pid);
            }
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_Comment.Contains(keyword));
                expression = expression.Or(t => t.F_CreatorUserName.Contains(keyword));
            }
            if (state != -1)
            {
                expression = expression.And(t => t.F_State == state);
            }
            return service.FindList(expression, pagination);
        }
        public CommentEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void Delete(string keyValue)
        {
            service.Delete(t => t.F_Id == keyValue);
        }

        public void SubmitForm(CommentEntity entity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                entity.Modify(keyValue);
                service.Update(entity);
            }
            else
            {
                entity.Create();
                service.Insert(entity);

                new UserStatisticsApp().AddCount("F_CommentCount");
            }
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="state">状态。1=审核通过, 2=审核不通过</param>
        public void Audit(string keyValue, int state)
        {
            var entity = service.FindEntity(keyValue);
            entity.Modify(keyValue);
            entity.F_State = state;
            if (state == 1)
            {
                entity.F_ReleaseTime = DateTime.Now;
            }
            else if (state == 2)
            {
                entity.F_ReleaseTime = null;
            }
            service.Update(entity);
            new UserStatisticsApp().AddCount("F_CommentAuditCount");
            new LogApp().WriteDbLog("评论管理", DbLogType.Update, string.Format("Id:{0},F_State为:{1}", entity.F_Id, state));
        }

        #region For front

        public List<CommentEntity> GetList(int pageIndex, string articleId)
        {
            var pagination = new Pagination
            {
                page = pageIndex,
                rows = 10,
                sidx = "F_CreatorTime desc",
                sord = "asc"
            };
            var expression = ExtLinq.True<CommentEntity>();
            expression = expression.And(t => t.F_ArticleId == articleId);
            expression = expression.And(t => t.F_State == 1);
            return service.FindList(expression, pagination);
        }

        #endregion
    }
}